package com.mz.mattress.client.api.biz;

import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.enums.FilePathEnum;
import com.mz.mattress.api.event.MyEvent;
import com.mz.mattress.api.redis.RedisKeys;
import com.mz.mattress.client.move.MoveService;
import com.mz.mattress.client.plan.PlanTimerService;
import com.mz.mattress.client.service.PlanDateService;
import com.mz.mattress.client.service.SysFileService;
import com.mz.mattress.client.util.WxQrcodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private SysFileService sysFileService;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private WxQrcodeUtil wxQrcodeUtil;

    @Autowired
    private PlanDateService planDateService;

    @Autowired
    private MoveService moveService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private PlanTimerService planTimerService;

    @GetMapping("/eventTest")
    public Result eventTest() {

        System.out.println(Thread.currentThread().getName());

        applicationContext.publishEvent(new MyEvent(this, "AA", "bb"));

        return Result.ok();
    }

    @GetMapping("/test")
    public Result test() {
        return Result.ok();
    }

    @GetMapping("/sendTask")
    public Result sendTask(@RequestParam("count") Integer count) {
        planTimerService.sendTask(count);
        return Result.ok();
    }

    @GetMapping("/sendCheckTask")
    public Result sendCheckTask(@RequestParam("count") Integer count) {
        planTimerService.sendCheckTask(count);
        return Result.ok();
    }

    @GetMapping("/monitorData")
    public Result monitorData() {
        Map<String, Integer> map = planDateService.monitorData();
        return Result.ok(map);
    }

    @GetMapping("/initOldMobile")
    public Result initOldMobile() {
        planDateService.initOldMobile();
        return Result.ok();
    }

    @GetMapping("/send")
    public Result send() {
        redisTemplate.boundListOps(RedisKeys.PLAN_TASK_QUEUE).rightPush(1);
        return Result.ok();
    }

    @GetMapping("/qrcode")
    public void qrcode(@RequestParam("code") String code, HttpServletResponse response) throws IOException {

//        String code = "67f7394cb92ce8bf0d68595c";
//        String url = "pages/device/deviceIndex/deviceIndex";

        byte[] bytes = wxQrcodeUtil.generateQrcodeByte(code, Constants.WX_DEVICE_PAGE, 128);

        // 设置contentType
        response.setContentType("image/png");

        // 写入response的输出流中
        OutputStream stream = response.getOutputStream();
        stream.write(bytes);
        stream.flush();
        stream.close();

    }

    @GetMapping("/help")
    public void help(HttpServletResponse response) {

        StringBuilder sb = new StringBuilder();
        sb.append("\r\n importData");
        String str = sb.toString();

        outputHtml(response, str);
    }

    private void outputHtml(HttpServletResponse response, String str) {
        response.setContentType("text/plain;charset=UTF-8");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        try {
            response.getWriter().write(str);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    //上传文件 图片
    @PostMapping(value = "/upload")
    public Result<String> upload(@RequestParam("filePath") Integer filePath, HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
        return sysFileService.upload(file, FilePathEnum.valueOf(filePath));
    }

    @GetMapping("/gen")
    public void gen() {
        moveService.gen();
    }

}
